<?php
namespace app\web\controller;

use think\Db;
use think\Request;
use app\common\controller\commonApi as comm;

/**
 * Class Menu
 * @package app\web\controller
 * by J.sky
 * 2020年5月5日11:05
 */
class Menu
{
    /**
     * 菜单列表
     * @param Request $request
     * by J.sky
     * 2020年5月5日11:54:05
     */
    public function menuList(Request $request){
        try {
            //获取管理员信息
            $adminInfo = $request->adminInfo;

            $menuList = Db::name("menu")->order('sort', 'desc')->select();
            $menuArr = getChild($menuList);
            ret(200, '成功', ["list"=>$menuList,"tree"=>$menuArr]);
        } catch (\Exception $e) {
            comm::oplog("sql错误",$adminInfo['id'],$adminInfo['username'],json_encode(['错误API'=>retMAC(),'error' => $e->getMessage(),'操作用户IP'=>get_ip()]));
            rLog(['operate' => 'sql错误', 'error' => $e->getMessage()]);
            ret(404, '服务器异常请联系管理员');
        }
    }

    /**
     * 修改菜单
     * @param Request $request
     * by J.sky
     * 2020年5月5日11:54:05
     */
    public function menuModify(Request $request){
        try {
            //获取管理员信息
            $adminInfo = $request->adminInfo;

            $id = input('post.id', '', 'trim');
            empty($id) ? ret(400, '请选择需要修改的菜单！') : '';
            $title = input('post.title', '', 'trim');
            empty($title) ? ret(400, '菜单名称不能为空！') : '';
            $icon = input('post.icon', '', 'trim');
            empty($icon) ? ret(400, '请选择菜单图标！') : '';
            $sort = input('post.sort', 0, 'trim');
            $ret = Db::name("menu")->where('id', $id)->update(['title' => $title,'icon'=>$icon,"sort"=>$sort]);
            $ret ? ret(200, '成功！') : ret(400, '修改失败');
        } catch (\Exception $e) {
            comm::oplog("sql错误",$adminInfo['id'],$adminInfo['username'],json_encode(['错误API'=>retMAC(),'error' => $e->getMessage(),'操作用户IP'=>get_ip()]));
            rLog(['operate' => 'sql错误', 'error' => $e->getMessage()]);
            ret(404, '服务器异常请联系管理员');
        }
    }
}
